package com.tuanzi.blog.web.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuanzi.blog.model.Tag;
import com.tuanzi.blog.service.TagService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

/**
 * 标签的管理
 */

@Controller
@RequestMapping("/admin")
public class TagsController {

    @Resource
    private TagService tagService;

    @GetMapping("/tags")
    public String list(@PageableDefault(page = 1,size = 6,sort = {"id"},
            direction = Sort.Direction.DESC) Pageable pageable,
                       Model model){
        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        List<Tag> list = tagService.getAllTags(); //得到从pageNum开始的pageSize条数据
        //如果直接返回list，得到了分页的数据，如果添加下面步骤，返回pageInfo，则能得到包括list在内的分页信息
        PageInfo<Tag> pageInfo = new PageInfo<>(list);

        model.addAttribute("page",pageInfo);

        return "admin/tags";
    }


    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tag-input";
    }

    //添加新的标签
    @PostMapping("/tags")
    public String add(Tag tag , RedirectAttributes redirectAttributes){
        int t = tagService.saveTag(tag);
        if (t == 3){
            redirectAttributes.addFlashAttribute("message","操作失败！该标签 "+ tag.getName() +" 已经存在！");
        }else if (t == 1){
            redirectAttributes.addFlashAttribute("message","新增标签 "+tag.getName()+" 成功");
        }else if (t ==3){
            redirectAttributes.addFlashAttribute("message","插入标签"+tag.getName()+"失败！！！");
        }
        return "redirect:/admin/tags";
    }

    //更改类型点击跳转
    @GetMapping("/tags/{id}/update")
    public String update(@PathVariable Long id, Model model){

        model.addAttribute("tag",tagService.getTag(id));
        System.out.println("============================================我跳转了!!!");

        return "admin/tag-input";
    }

    //更改类型写入数据
    @PostMapping("/tags/{id}")
    public String add(Tag tag ,@PathVariable Long id, RedirectAttributes redirectAttributes){
        int t = tagService.updateTag(id,tag);
        if (t == 1){
            redirectAttributes.addFlashAttribute("message","更新成功,标签更新为 : "+tag.getName());
        }else if (t == 2) {
            redirectAttributes.addFlashAttribute("message","更新标签"+tag.getName()+"失败！！！");
        }else if(t == 3 ){
            redirectAttributes.addFlashAttribute("message","该标签 "+tag.getName()+" 已存在，不需要更新！");
        }
        return "redirect:/admin/tags";
    }



    //删除类型
    @GetMapping("/tags/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
        Tag tag = tagService.deleteTag(id);
        if (tag != null){
            redirectAttributes.addFlashAttribute("message",tag.getName()+" 标签已删除!");
        }else {
            redirectAttributes.addFlashAttribute("message","删除失败！！！");
        }
        return "redirect:/admin/tags";
    }


}
